package com.fang.networkdisk.controller;

import com.fang.common.common.CommonPage;
import com.fang.common.common.CommonResult;
import com.fang.common.request.BaseController;
import com.fang.common.util.CommonResultUtil;
import com.fang.networkdisk.entity.bo.UserBo;
import com.fang.networkdisk.entity.dto.UserDto;
import com.fang.networkdisk.entity.vo.UserVo;
import com.fang.networkdisk.service.TbNetDisUserService;
import com.fang.networkdisk.util.NDBeanUtils;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;

/**
 * @Description
 * @Author Bernie_fang
 * @Date 2025/3/1 21:07
 **/
@RestController
@RequestMapping("/system/user")
public class UserController extends BaseController {

    @Resource
    private TbNetDisUserService tbNetDisUserService;

    @PostMapping("/list")
    public CommonResult<CommonPage<List<UserVo>>> queryUser(@RequestBody UserDto userDto) {
        UserBo userBo = new UserBo();
        NDBeanUtils.copyListProperties(userDto, userBo);
        CommonPage result = tbNetDisUserService.queryUser(userBo);
        return CommonResultUtil.success(result);
    }

    @PostMapping("/add")
    public CommonResult<CommonPage<List<UserVo>>> addUser(@RequestBody UserDto userDto) {
        String authorId = super.getAuthorId();
        UserBo userBo = new UserBo();
        NDBeanUtils.copyListProperties(userDto, userBo);
        userBo.setCreateUser(authorId);
        userBo.setUpdateUser(authorId);
        tbNetDisUserService.addUser(userBo);
        return CommonResultUtil.success();
    }

    /**
     * Description: 修改用户账号信息，不修改用户id，用户名，密码
     * @Author: Bernie_fang
     * @Since: 2025/3/1 22:15
     * @param userDto:
     * @return: com.fang.common.common.CommonResult<com.fang.common.common.CommonPage<java.util.List<com.fang.networkdisk.entity.vo.UserVo>>>
     **/
    @PostMapping("/update")
    public CommonResult<CommonPage<List<UserVo>>> updateUser(@RequestBody UserDto userDto) {
        String authorId = super.getAuthorId();
        UserBo userBo = new UserBo();
        NDBeanUtils.copyListProperties(userDto, userBo);
        userBo.setCreateUser(null);
        userBo.setUpdateUser(authorId);
        tbNetDisUserService.updateUserById(userBo);
        return CommonResultUtil.success();
    }

    @GetMapping("/delete/{id}")
    public CommonResult<CommonPage<List<UserVo>>> deleteUser(@PathVariable("id") Integer id) {
        String authorId = super.getAuthorId();
        tbNetDisUserService.deleteUserById(id, authorId);
        return CommonResultUtil.success();
    }

}
